import config from '@/config/index.js'
export function setToken(token) {
	return uni.setStorageSync('token', token);
}
export function removeToken() {
	return uni.removeStorageSync('token');
}
export function getToken() {
	let token = uni.getStorageSync('token')
	return token
}

export function setCache(k, v) {
	return uni.setStorageSync(k, v)
}

export function getCache(k) {
	return uni.getStorageSync(k)
}

export function delCache(k) {
	return uni.removeStorageSync(k)
}
export function getApi(path) {
	return config.api_base_url + path
}
// 生成url
export function genUrl(url, query) {
	if (JSON.stringify(query) == "{}") {
		return url;
	}
	if (url.indexOf('?') == -1) {
		url += '?'
	} else {
		url += '&'
	}
	for (var key in query) {
		if (query[key] !== null) {
			url += (key + '=' + encodeURIComponent(query[key]) + '&');
		}
	}
	return url.substring(0, url.lastIndexOf('&'));
}

export function arrayGroup(data, len) {
	let result = [];
	for (let i = 0; i < data.length; i += len) {
		result.push(data.slice(i, i + len));
	}
	return result;
}
export function tipBreak(msg) {
	uni.showToast({
		title: msg,
		icon: 'none'
	})
	throw new Error(msg)
}
export function tipBack(msg, t = 2000) {
	uni.showToast({
		title: msg,
		icon: 'none',
		success() {
			setTimeout(function() {
				uni.navigateBack()
			}, t)
		}
	})
}
export function tipTopage(msg, path = '') {
	uni.showToast({
		title: msg,
		icon: 'none',
		success() {
			if (path) {
				console.log(path)
				setTimeout(function() {
					uni.navigateTo({
						url: path
					})
				}, 2000)
			}
		}
	})
	throw new Error(msg)
}
export function tipSwitchTabTopage(msg, path = '', d = 2000) {
	uni.showToast({
		title: msg,
		icon: 'none',
		success() {
			if (path) {
				setTimeout(function() {
					uni.switchTab({
						url: path
					})
				}, d)
			}
		}
	})
	throw new Error(msg)
}
export function toPage(path) {
	uni.navigateTo({
		url: path,
	})
}
export function switchTabTopage(path) {
	uni.switchTab({
		url: path
	})
}

export function getConfig(k) {
	return config[k]
}

export function formatSeconds(value) {
	let second = parseInt(value)
	let minute = 0
	let hour = 0
	if (second > 60) {
		minute = parseInt(second / 60)
		second = parseInt(second % 60)
		if (minute > 60) {
			hour = parseInt(minute / 60)
			minute = parseInt(minute % 60)
		}
	}

	let result = '' + parseInt(second) + '秒'
	if (minute > 0) {
		result = '' + parseInt(minute) + '分' + result
	}
	if (hour > 0) {
		result = '' + parseInt(hour) + '小时' + result
	}
	return result
}

export function is_weixn() {
	if (!config.check_wechat) {
		return true
	}
	var ua = navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger" && navigator.userAgent.indexOf('Mobile') > -1) {
		return true;
	} else {
		return false;
	}
}
export function validatePhoneNumber(number) {
	var regex = /^1[3456789]\d{9}$/;
	return regex.test(number);
}
export function uploadImage(upFileUrl){
    return new Promise(function (resolve, reject) {
       uni.uploadFile({
				url: getApi('/api/upload/image'),
				name: 'file',
				header: {
					'Authorization': getToken(),
					'X-Requested-With': 'XMLHttpRequest'
				},
                filePath: upFileUrl,//上传文件地址
                success: (res) => {
					let data = JSON.parse(res.data)
                    resolve(data.data);//成功返回，resolve是Promise的回调方式
                },
                fail(res) {
                    uni.showToast({
                        title: '网络请求失败',
                        icon: 'none'
                    });
                    reject(res);//失败返回，resolve是Promise的回调方式
                }
        });
    })
}